﻿
using IBll;
using IDal;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Bll
{
    /// <summary>
    /// 继承 康复理疗接口方法
    /// </summary>
    public class SosConeBll : ISosConebLL
    {
        /// <summary>
        /// 实现方法
        /// </summary>
        /// <param name="baserDa"> 康复理疗</param>
        /// <param name="baserD">分类</param>
        public SosConeBll(IBaserDal<SosCone> baserDa,IBaserDal<SosConeType> baserD)
        {
            BaserDa = baserDa;
            BaserD = baserD;
        }

        public IBaserDal<SosCone> BaserDa { get; }
        public IBaserDal<SosConeType> BaserD { get; }


        /// <summary>
        /// 显示查询分页
        /// </summary>
        /// <param name="name">项目名称</param>
        /// <param name="index">页数</param>
        /// <param name="size">条数</param>
        /// <returns>通过查询姓名名称获取一条 数据</returns>
        public Dto ShowSosCone(string? name, int index, int size)
        {

            try//异常处理
            {
                var sosCone = BaserDa.GetValues().Where(x=>x.isDelete==false );
                var sosConeType = BaserD.GetValues();

                var linq = from a in sosCone
                           join b in sosConeType
                           on a.SosConeTypeId equals b.SosConeTypeId
                           select new
                           {
                               a.SosConeType,
                               a.SosConeSC,
                               a.SosConeTypeId,
                               a.SosConeNum,
                               a.Code,
                               a.Price,
                               a.SosConeNmae,
                               a.SosConeId,
                               b.SosConeName,
                               a.State,


                           };

                if (!string.IsNullOrEmpty(name))
                {
                     linq=linq.Where(x=>x.SosConeNmae.Contains(name));
                }

                 int p=linq.Count();
                var o = linq.OrderBy(x => x.SosConeId).Skip((index - 1) * size).Take(size).ToList();
                return new Dto { Count = p, Mumber = o };


            }
            catch (Exception)
            {

                throw;
            }
       
             

        }
        /// <summary>
        /// 新增康复理疗
        /// </summary>
        /// <param name="t">数据信息</param>
        /// <returns>返回值受影响行数</returns>
        public async Task<int> AddAnync(SosCone sosCone)
        {
            try//异常处理
            {
                var e = DateTime.Now.ToString("yyyyMMdd");
                var k = new Random().Next(100, 1000).ToString();
                sosCone.Code = e + k;
                return await BaserDa.AddAnync(sosCone);
            }
            catch (Exception)
            {

                throw;
            }
      

        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>通过id查询一条数据</returns>
        public async Task<int> DeleteState(int id)
        {
            try//异常处理
            {
                var uu = BaserDa.GetValues().FirstOrDefault(x => x.SosConeId == id);
                if (uu.isDelete == true)
                {
                    uu.isDelete = false;
                }
                else
                {
                    uu.isDelete = true;
                }
                return await BaserDa.UpdateAnync(uu);
            }
            catch (Exception)
            {

                throw;
            }
           
          

        }


        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="idsstaings">主键id</param>
        /// <returns>通过id查询一条数据</returns>
        public async Task<int> Pstate(string idsstaings)
        {
            try//异常处理
            {
                 var ids = idsstaings.Split(",").Select(x => Convert.ToInt32(x)).ToList();
                var iss = BaserDa.GetValues().Where(x =>ids.Contains(x.SosConeId));
                foreach (var item in iss)
                {
                    item.State =item.State==true?false:true;    

                 
                }
                return await BaserDa.UpdateRangeAnync(iss.ToList());
            }

            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns>返回受影响行数</returns>
        public List<SosCone> GetSosCones()
        {
            try//异常处理
            {
                 return BaserDa.GetValues().ToList();



            }
            catch (Exception)
            {

                throw;
            }
            
        }


    }
}
